package com.atguigu.gmall.product.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.BaseManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.NamedBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "后台管理界面")
@RestController
//@CrossOrigin
@RequestMapping("/admin/product")
public class BaseManageController {

    @Autowired
    BaseManageService baseManageService;


    @GetMapping("getCategory1")
    @ApiOperation("一级分类查询")
    public Result getCategory1(){
        List<BaseCategory1> category1List = baseManageService.getCategory1();
         return Result.ok(category1List);
    }

   // http://api.gmall.com/admin/product/getCategory2/{category1Id}
    @GetMapping("getCategory2/{category1Id}")
    @ApiOperation("二级分类查询")
    public Result getCategory2(@PathVariable(name = "category1Id") Long category1Id){
        List<BaseCategory2> category2List = baseManageService.getCategory2(category1Id);
        return Result.ok(category2List);

    }

    //http://api.gmall.com/admin/product/getCategory3/{category2Id}
    @GetMapping("getCategory3/{category2Id}")
    @ApiOperation("三级分类查询")
    public Result getCategory3(@PathVariable(name = "category2Id") Long category2Id){
        List<BaseCategory3> category3List = baseManageService.getCategory3(category2Id);
        return Result.ok(category3List);
    }

    //http://api.gmall.com/admin/product/attrInfoList/{category1Id}/{category2Id}/{category3Id}
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    @ApiOperation("获取平台属性")
    public Result attrInfoList(
            @PathVariable(name = "category1Id") Long category1Id,
            @PathVariable(name = "category2Id") Long category2Id,
            @PathVariable(name = "category3Id") Long category3Id
    ){
        List<BaseAttrInfo> baseAttrInfosList = baseManageService.attrInfoList(category1Id,category2Id,category3Id);
        return Result.ok(baseAttrInfosList);
    }

    //http://api.gmall.com/admin/product/saveAttrInfo
    @PostMapping("saveAttrInfo")
    @ApiOperation("添加平台属性")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo attrForm){

            if (attrForm.getId() == null){
                //添加平台属性
                baseManageService.saveAttrInfo(attrForm);
                return Result.ok();
            }
       //更新数据
       baseManageService.updateAttrInfo(attrForm);
        return Result.ok();
    }

    //http://api.gmall.com/admin/product/getAttrValueList/{attrId}
    @GetMapping("getAttrValueList/{attrId}")
    @ApiOperation("根据平台id获取平台属性")
    public Result getAttrValueList(@PathVariable(name = "attrId") Long attrId){
        List<BaseAttrValue> baseAttrValues = baseManageService.getAttrValueList(attrId);
        return Result.ok(baseAttrValues);
    }

    //http://api.gmall.com/admin/product/baseTrademark/{page}/{limit}
    @GetMapping("baseTrademark/{page}/{limit}")
    @ApiOperation("品牌列表分页查询")
    public Result baseTrademark(
            @PathVariable(name = "page") Long page,
            @PathVariable(name = "limit") Long limit
            ){
        IPage<BaseTrademark> baseTrademarkIPage = baseManageService.baseTrademark(page, limit);
        return Result.ok(baseTrademarkIPage);
    }

    //http://api.gmall.com/admin/product/{page}/{limit}?category3Id=61
    @GetMapping("/{page}/{limit}")
    @ApiOperation("spu属性查询")
    public Result SpuFile(
            @PathVariable(name = "page") Long page,
            @PathVariable(name = "limit") Long limit,
            @RequestParam Long category3Id){
        IPage<SpuInfo> spuInfoIPage =  baseManageService.SpuFile(page,limit,category3Id);
        return Result.ok(spuInfoIPage);
    }

    //http://api.gmall.com/admin/product/baseSaleAttrList
    @GetMapping("baseSaleAttrList")
    @ApiOperation("获取销售属性")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr> saleAttrList = baseManageService.baseSaleAttrList();
        return Result.ok(saleAttrList);
    }

    //http://api.gmall.com/admin/product/baseTrademark/getTrademarkList
    @GetMapping("baseTrademark/getTrademarkList")
    @ApiOperation("获取品牌属性")
    public Result getTrademarkList(){
        List<BaseTrademark> baseTrademarkList = baseManageService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }

    //http://api.gmall.com/admin/product/saveSpuInfo
    @PostMapping("saveSpuInfo")
    @ApiOperation("添加spu属性")
    public Result saveSpuInfo(@RequestBody SpuInfo spuForm){
        baseManageService.saveSpuInfo(spuForm);
        return Result.ok();
    }

    //http://www.gmall.com/admin/product/saveSkuInfo
    @PostMapping("saveSkuInfo")
    @ApiOperation("添加详细sku属性")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        baseManageService.saveSkuInfo(skuInfo);
        System.out.println(skuInfo);
        return Result.ok();
    }
    //SkuInfo(spuId=4, price=100.00, skuName=名称, skuDesc=描述, weight=10, tmId=2, category3Id=61,
    // skuDefaultImg=http://192.168.200.128:8080/group1/M00/00/00/wKjIgF-0Ez2ARm0ZAANJjSt4ynk574.jpg, isSale=null,
    // skuImageList=[
    // SkuImage(skuId=null, imgName=032be1072bde8b82.jpg, imgUrl=.jpg, spuImgId=47, isDefault=1),
    // SkuImage(skuId=null, imgName=2debc304832f91bb.jpg, imgUrl=.jpg, spuImgId=48, isDefault=0),
    // SkuImage(skuId=null, imgName=5c427fea652c1db4.jpg, imgUrl=.jpg, spuImgId=52, isDefault=0),
    // SkuImage(skuId=null, imgName=ed84fc94dfb5a0bc.jpg, imgUrl=.jpg, spuImgId=55, isDefault=0),
    // SkuImage(skuId=null, imgName=dc9ee460a3b9688b.jpg, imgUrl=.jpg, spuImgId=56, isDefault=0),
    // SkuImage(skuId=null, imgName=d4fe45cbc1e0b231.jpg, imgUrl=.jpg, spuImgId=58, isDefault=0)],
    // skuAttrValueList=[
    // SkuAttrValue(attrId=1, valueId=6, skuId=null, baseAttrInfo=null, baseAttrValue=null),
    // SkuAttrValue(attrId=2, valueId=12, skuId=null, baseAttrInfo=null, baseAttrValue=null),
    // SkuAttrValue(attrId=3, valueId=19, skuId=null, baseAttrInfo=null, baseAttrValue=null),
    // SkuAttrValue(attrId=4, valueId=27, skuId=null, baseAttrInfo=null, baseAttrValue=null),
    // SkuAttrValue(attrId=5, valueId=45, skuId=null, baseAttrInfo=null, baseAttrValue=null),
    // SkuAttrValue(attrId=6, valueId=47, skuId=null, baseAttrInfo=null, baseAttrValue=null),
    // SkuAttrValue(attrId=7, valueId=49, skuId=null, baseAttrInfo=null, baseAttrValue=null)],
    // skuSaleAttrValueList=[
    // SkuSaleAttrValue(skuId=null, spuId=null, saleAttrValueId=22),
    // SkuSaleAttrValue(skuId=null, spuId=null, saleAttrValueId=24)])



    //http://www.gmall.com/admin/product/spuSaleAttrList/1
    @GetMapping("spuSaleAttrList/{spuId}")
    @ApiOperation("根据销售ID查属性")
    public Result spuSaleAttrList(@PathVariable(name = "spuId") Long spuId){
    List<SpuSaleAttr> saleAttrList = baseManageService.spuSaleAttrList(spuId);
        return Result.ok(saleAttrList);
    }
    //http://www.gmall.com/admin/product/spuImageList/4
    @GetMapping("spuImageList/{spuId}")
    @ApiOperation("根据销售ID查询图片")
    public Result spuImageList(@PathVariable(name = "spuId") Long spuId){
        List<SpuImage> spuImageList = baseManageService.spuImageList(spuId);
        return Result.ok(spuImageList);
    }

    //http://www.gmall.com/admin/product/list/1/10
    @GetMapping("list/{page}/{limit}")
    @ApiOperation("分页查询sku属性")
    public Result list(
            @PathVariable(name = "page") Long page,
            @PathVariable(name = "limit") Long limit
    ){
        IPage<SkuInfo> skuInfoIPage = baseManageService.list(page,limit);
        System.out.println(skuInfoIPage);
        return Result.ok(skuInfoIPage);
    }


    // http://www.gmall.com/admin/product/onSale/15
    @GetMapping("onSale/{id}")
    @ApiOperation("上架")
    public void onSale(@PathVariable(name = "id") Long id){
        baseManageService.onSale(id);
        return;
    }

    @GetMapping("cancelSale/{id}")
    @ApiOperation("下架")
    public void cancelSale(@PathVariable(name = "id") Long id){
        baseManageService.cancelSale(id);
        return;
    }

}
